﻿using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Configuration;

namespace Cloudflare_Dynamic_IP
{
    public partial class Form1 : Form
    {
        Boolean autoStatus = false;

        public Form1()
        {
            InitializeComponent();    
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            textBox1.Text = ConfigurationManager.AppSettings["api"];
            textBox2.Text = ConfigurationManager.AppSettings["email"];
            textBox3.Text = ConfigurationManager.AppSettings["zone"];
            String interval = ConfigurationManager.AppSettings["time"];
            if (String.Compare(interval, "300") + String.Compare(interval, "600") + String.Compare(interval, "1200") + String.Compare(interval, "3600") == 3)
            {
                comboBox1.Text = interval;
                timer1.Interval = Int32.Parse(interval) * 1000;
            }
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            textBox5.Text = await DataClass.GetPublicIP();
            AddMessage(listView2, await DataClass.GetDomainRecords(textBox1.Text,textBox2.Text,textBox3.Text,listView1));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (autoStatus == false)
            {
                foreach (TextBox temp in new TextBox[] { textBox1, textBox2, textBox3, textBox5 })
                    temp.ReadOnly = true;
                timer1.Enabled = true;
                autoStatus = true;
                button1.Enabled = false;
                comboBox1.Enabled = false;
                button2.BackColor = System.Drawing.Color.Green;
                button2.Text = "Pause";
                AddMessage(listView2, "Timer Start!");
            }
            else
            {
                foreach (TextBox temp in new TextBox[] { textBox1, textBox2, textBox3, textBox5 })
                    temp.ReadOnly = false;
                timer1.Enabled = false;
                autoStatus = false;
                button1.Enabled = true;
                comboBox1.Enabled = true;
                button2.BackColor = System.Drawing.Color.Red;
                button2.Text = "Start Auto Update And Refresh";
                AddMessage(listView2, "Timer Pause!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listView2.Items.Clear();
        }

        private async void timer1_Tick(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                textBox5.Text = await DataClass.GetPublicIP();
            }
            AddMessage(listView2, await DataClass.GetDomainRecords(textBox1.Text, textBox2.Text, textBox3.Text, listView1));
            foreach (String tempeResult in await DataClass.ChangeDomainRecords(textBox1.Text, textBox2.Text, textBox3.Text, listView1, textBox5.Text))
            {
                AddMessage(listView2, tempeResult);
            }
            AddMessage(listView2, await DataClass.GetDomainRecords(textBox1.Text, textBox2.Text, textBox3.Text, listView1));
        }

        private void AddMessage(ListView list, String message)
        {
            ListViewItem item = new ListViewItem();
            item.Text = System.DateTime.Now.ToString();
            item.SubItems.Add(message);
            list.Items.Add(item);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://clordflaredynamic.codeplex.com/"); 
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            timer1.Interval = Int32.Parse(comboBox1.Text) * 1000;
        }
    }
}
